package com.zzxx.wechat.handle;

import com.zzxx.wechat.dao.ArcteryxBindDao;
import com.zzxx.wechat.entity.ArcteryxBind;
import com.zzxx.wechat.util.URLValidator;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class TextHandler implements WxMpMessageHandler {

/*
    @Autowired
    CommandService commandService;
*/

    @Autowired
    ArcteryxBindDao arcteryxBindDao;


    @Value("${wx.domain}")
    private String wxDomain;


    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
        // 接收的消息内容
        String inContent = wxMessage.getContent();
        // 响应的消息内容
        String outContent = null;

        if (!URLValidator.isValid(inContent) || !inContent.contains("arcteryx")) {
            return WxMpXmlOutMessage.TEXT().content("输入链接不正确,请输入正确arcteryx商品地址").fromUser(wxMessage.getToUser())
                    .toUser(wxMessage.getFromUser()).build();
        }
        ArcteryxBind arcteryxBind = new ArcteryxBind();

        arcteryxBind.setSpuUrl(inContent);
        arcteryxBindDao.save(arcteryxBind);


        List<WxMpXmlOutNewsMessage.Item> articles = new ArrayList<>();
        WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
        item.setDescription("点击查看订阅结果");
        item.setPicUrl("https://mmbiz.qpic.cn/sz_mmbiz_jpg/KmEVtAFTgT3beyTXYgTicGmicVMtDveRl3OO0avNAWm8Aoo5iaeWvefTia6Gg83AackD4Me8ZryZv89R76aMw4icx4Q/640?wx_fmt=jpeg&amp;from=appmsg");
        item.setTitle("已订阅");
        item.setUrl(wxDomain + "/index?bindId=" + arcteryxBind.getId());
        articles.add(item);
        return WxMpXmlOutMessage
                .NEWS()
                .addArticle(item)
                .articles(articles)
                .fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser())
                .build();

/*        if (WechatCommandEnum.QUERY.getCommand().equals(inContent)) {
            outContent = commandService.queryAttention(wxMessage.getFromUser());
        }
        if (inContent.startsWith(WechatCommandEnum.ADD.getCommand())) {
            outContent = commandService.addCommand(wxMessage.getFromUser(),inContent.substring(4));
        }
        if (inContent.startsWith(WechatCommandEnum.CANCEL.getCommand())) {
            outContent = commandService.calCommand(wxMessage.getFromUser(),inContent.substring(7));
        }
        outContent = StringUtils.isEmpty(outContent) ? WechatConstant.WECHAT_HELP_INFO : outContent;
        // 构造响应消息对象
        return WxMpXmlOutMessage.TEXT().content(outContent).fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser()).build();*/
    }


}